package org.bigjava.user.service.impl;

import org.bigjava.bean.Posts;
import org.bigjava.bean.User;
import org.bigjava.common.Constant;
import org.bigjava.user.mapper.UserMappers;
import org.bigjava.user.service.UserServices;
import org.bigjava.user.util.AESUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
public class UserServicesImpl implements UserServices {
    java.util.Date date = new Date();//获取当前时间
    Timestamp t = new Timestamp(date.getTime());
    AESUtil aesUtil = new AESUtil();//aes工具类

//    Consumer<User> user =(username)->{
//        System.out.println(username);
//    };

    @Autowired
    UserMappers mapper;

    @Override
    public List<User> getAllUser() {


        //获取全部用户
        return mapper.getAllUser();
    }

    @Transactional
    @Override
    public boolean sign(User user) {
        //注册用户
        int flag = mapper.queryUser(user.getUserName());
        if (flag != 0) return false;
        int petname = (int) (Math.random() * 1000);
        user.setUserDate(t);
        user.setPassword(aesUtil.encrypt(user.getPassword()));//密码加密
        user.setUserHead(Constant.defaulthead);//设置初始头像
        user.setUserSex("无");
        user.setPetName(Constant.defaultPetName + petname);//设置初始昵称
        user.setUserLikes(0);//设置初始值
        user.setUserState(0);//设置初始状态
        user.setUserVip(0);//设置普通用户权限
        mapper.sign(user);
        return true;
    }

    @Override
    public User getUserById(long id) {
        //根据用户id获取用户资料
        User u = mapper.getUserById(id);
        return u;
    }

    public boolean queryUserName(String userName) {
        //查询用户名是否重复
        int flag = mapper.queryUser(userName);
        System.out.println(userName + "----" + flag);
        if (flag == 0) {
            return true;
        } else {
            return false;
        }

    }

    @Override
    public boolean loginUser(User user) {

        //登录验证

        user.setPassword(aesUtil.encrypt(user.getPassword()));
        int flag = mapper.queryUserlogin(user.getUserName(), user.getPassword());
        System.out.println(flag);
        if (flag == 0)
            return false;
        else
            return true;
    }

    @Override
    public User getUserByName(String userName) {
        //根据用户名获取用户资料

        return mapper.getUserByName(userName);
    }

    @Transactional
    @Override
    public void updateUserlikes(long id) {
        System.out.println("servicelike");
        mapper.updateUserlikes(id);
    }

    @Override
    public void updateUserNolikes(long id) {
        mapper.updateUserNolikes(id);
    }

    @Override
    public int queryUserState(long userId) {
        return mapper.queryUserState(userId);
    }

    @Override
    public List<Posts> querytopicOfPosts(long topicId) {

        return null;
    }

    @Override
    public User getUserbyidother(String username) {
        return mapper.getUserByothersName(username);
    }

}
